<template>
  <BasicLayout>
    <template #wrapper>
      <search-form
        :report-type="formData.reportType"
        :is-goods="true"
        :is-single-date="true"
        :expdrop="true"
        :exporting="exporting"
        @submit="onSearchReport"
        @export="val => onExportReport('城市门店报表', val,exportApi)"
      />
      <tree-table
        :loading="loading"
        :columns="columns"
        :table-data="tableData"
        :load="loadChildNodes"
        :tree-node-prop="'hasChildNodes'"
        border
      />
    </template>
  </BasicLayout>
</template>
<script>
import SearchForm from '@/components/business/SearchForm';
import TreeTable from '@/components/common/TreeTable';
import { cityColumns as Columns } from '@/utils/columns-config/hic-header-config';
import exportReportMixin from '@/components/mixins/ExportReport';
import { CyGetReport as getPerformanceReport, CyExport } from '@/api/hinApi';

export default {
  name: 'KAliveHIVBusinessdDstrict',
  components: {
    SearchForm,
    TreeTable
  },
  mixins: [exportReportMixin],
  data() {
    return {
      exportApi: CyExport,
      columns: Columns,
      tableData: [],
      loading: false,
      exporting: false,
      formData: {
        export: 0, // 导出字段，1为excel,2为csv
        type: 2 // 2为城市版
        // queryMode: 'BUSINESS_CATEGORY',
        // reportType: 'BUSINESS_CATEGORY_REGION_CITY_SHOP'
      }
    };
  },
  methods: {
    // 导出
    // onExport() {
    //   console.log(arguments);
    // },
    onSearchReport(form) {
      this.loading = true;
      this.tableData = [];
      this.formData.export = 0;
      this.$nextTick(() => {
        this.formData = {
          ...this.formData,
          ...form
        };
        getPerformanceReport(this.formData)
          .then(({ data, resultCode }) => {
            if (resultCode === '0000') {
              data.forEach(item => {
                item.uuid =
                  item.id + 'HIV' + new Date().getTime();
                item.hasChildNodes = item.rowLevel < 3 ? 1 : 0;// 由rowLevel判断是否存在下级 rowLevel为3时，无下级
              });
              // console.log(data);
              this.tableData = data;
              this.loading = false;
            }
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    loadChildNodes(tree, treeNode, resolve) {
      const { queryMode, businessId, childQueryMode, id } = tree;
      const params = {
        ...this.formData,
        levels: id // 传父级ID
        // queryMode: childQueryMode
      };
      if (queryMode === 'BUSINESS_CATEGORY') {
        params.businessCategories = [businessId];
        this.businessCategories = params.businessCategories;
      } else if (queryMode === 'REGION') {
        params.businessCategories = this.businessCategories;
        params.regions = [businessId];
        this.regions = params.regions;
      } else if (queryMode === 'CITY') {
        params.businessCategories = this.businessCategories;
        params.regions = this.regions;
        params.cities = [parseInt(businessId)];
      }
      getPerformanceReport(params).then(({ data, resultCode }) => {
        if (resultCode === '0000') {
          data.forEach(item => {
            item.uuid =
                  item.id + 'HIV' + new Date().getTime();
            item.hasChildNodes = item.rowLevel < 3 ? 1 : 0;
          });
          resolve(data);
        }
      });
    }
  }
};
</script>
